/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.bullionxpress.server.impl;

import com.bullionxpress.server.application.Server;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author BullionXpress.com
 */
public class DatabaseManager {

    static Connection conn = null;

    public static Connection getConnection() {
        try {
            Class.forName(Server.getProperty("driver"));
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        try {
            if ((conn != null) && (!conn.isClosed())) {
                return conn;
            }
        } catch (SQLException ex) {
            //Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            conn = (Connection) DriverManager.getConnection(
                    Server.getProperty("connectionString"),
                    Server.getProperty("userid"),
                    Server.getProperty("password"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return conn;

    }
    

    public boolean depositAddressExisits(String address)
    {
        try { 
            conn = getConnection();
            Statement statement = conn.createStatement();
            ResultSet r = statement.executeQuery("SELECT * FROM accounts WHERE deposit_address = '" + address + "'");
            if( r.next() ) { 
                return true;
            }
        } catch(Exception ex) {
                 
        }
        return false;       
    }
    

    public void deposit(double d, String depositAddress) {
        try {
            conn = getConnection();
            Statement statement = conn.createStatement();
            double dNewCoins = d;
            String sql = "UPDATE accounts SET balance = balance + " + dNewCoins + " where deposit_address='" + depositAddress + "'";
            int n = statement.executeUpdate(sql);
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void withdraw(double d, String depositAddress) {
        try {
            conn = getConnection();
            Statement statement = conn.createStatement();
            double dNewCoins = d;
            String sql = "UPDATE accounts SET balance = balance - " + dNewCoins + " where deposit_address='" + depositAddress + "'";
            int n = statement.executeUpdate(sql);
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    
        
}
